package com.yxhl.km.module.home;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.yxhl.km.R;
import com.yxhl.km.base.BaseFragment;
import com.yxhl.km.module.home.adapter.HomeAdapter;
import com.yxhl.km.module.home.entity.HomeBean;
import com.yxhl.km.module.home.mvp.PersonPrensenter;
import com.yxhl.km.module.utils.LogUtil;
import com.yxhl.km.module.view.DiagUtil;
import com.yxhl.km.mvp.BaseView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张亚楠 on 2018/5/28.
 */


public class HomeFragment extends BaseFragment {
    private List<HomeBean.DataBeanX.DataBean> list = new ArrayList<>();
    private LRecyclerView mRecyclerView;
    private HomeAdapter mDataAdapter = null;
    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;
    private int page = 0;
    private int pageSize = 15;
    private int requestSize = 15;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initListener() {
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                RefreshUpdata();
            }
        });

        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page += 15;
                requstList();
            }
        });

    }

    private void RefreshUpdata() {
        initData();
        requstList();
    }

    @Override
    protected void initialized() {
        initData();
        initView();
    }

    private void initView() {
        initTitle("", "收获计划", "");
        initList();
        initHead();
        requstList();
    }

    private void requstList() {
        DiagUtil.getInstance().createWaitDialog(getActivity());
        PersonPrensenter personPrensenter = new PersonPrensenter(new BaseView<HomeBean>() {
            @Override
            public void onResult(HomeBean homeBean) {
                DiagUtil.getInstance().closeWaitDialog();
                if (null != homeBean) {
                    if (page == 0) {
                        mDataAdapter.clear();
                    }
                    updateUi(homeBean);
                }
            }
        });
        personPrensenter.getListData(page + "", pageSize + "");
    }

    private void updateUi(HomeBean homeBean) {
        if (mDataAdapter != null) {
            mDataAdapter.addAll(homeBean.getData().getData());
            requestSize = homeBean.getData().getData().size();
            if (requestSize != pageSize) {
                mRecyclerView.setNoMore(true);
            }
        }
        mLRecyclerViewAdapter.notifyDataSetChanged();
        mRecyclerView.refreshComplete(15);
    }

    private void initData() {
        list.clear();
        requestSize = 15;
        page = 0;
        pageSize = 15;
    }

    private void initList() {
        mRecyclerView = (LRecyclerView) view.findViewById(R.id.list);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(manager);
        mDataAdapter = new HomeAdapter(getActivity());
        mDataAdapter.addAll(list);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        //设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        //设置底部加载文字提示
        mRecyclerView.setFooterViewHint("拼命加载中", "", "网络不给力啊，点击再试一次吧");
        //根据需要选择使用GridItemDecoration还是SpacesItemDecoration
        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.home_diveider)
                .setPadding(R.dimen.default_divider_padding)
                .setColorResource(R.color.main_bg)
                .build();
//        GridItemDecoration divider = new 数据加载完毕GridItemDecoration.Builder(getActivity())
//                .setHorizontal(R.dimen.default_divider_padding)
//                .setVertical(R.dimen.default_divider_padding)
//                .setColorResource(R.color.main_bg)
//                .build();
        mRecyclerView.addItemDecoration(divider);
        //是否允许加载
        mRecyclerView.setLoadMoreEnabled(true);
        //是否允许刷新
        mRecyclerView.setPullRefreshEnabled(true);
    }

    private void initHead() {
        final View header = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home_head, (ViewGroup) getActivity().findViewById(android.R.id.content), false);
        mLRecyclerViewAdapter.addHeaderView(header);
    }

    @Override
    public void onClick(View v) {

    }
}
